﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System

Partial Public Class Service
#Region "物业单_获取客户列表"
    Public Function _Bill_Prop_GetCustomList(ByVal FSession As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._Bill_Prop_GetCustomList
        If FStyle.Trim = String.Empty Then
            ErrString = "参数不正确"
            Return False
        End If
        If FSession Is Nothing Then
            ErrString = "验证用户失败!"
            Return False
        End If
        '--验证用户
        If DControl._GetUserBySession(FSession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            Select Case FStyle.Trim
                Case "*"
                    sqlstr = "select * from Base_Property where FType=2"
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = String.Format("获取客户信息失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_获取客户信息"
    Public Function _BILL_Prop_GetCustInfo(ByVal FSession As String, ByVal FID As String, ByVal FName As String, ByRef FCInfo As FBaseProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_GetCustInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Base_Property"
            Dim ds As New DataSet
            If FID.Trim <> String.Empty Then
                '--按ID
                sqlstr += " where id=" & FID.Trim
            ElseIf FName.Trim <> String.Empty Then
                '--按客户名
                sqlstr += String.Format(" where FName='{0}'", FName.Trim)
            Else
                ErrString = FControl.GetErrString("取客户信息失败", "参数错误")
                Return False
            End If
            sqlstr += " and FType=2"
            '--执行查询
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("取客户信息失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("取客户信息失败!", "未找到指定项目")
                Return False
            End If
            Dim t As DataRow = ds.Tables(0).Rows(0)
            FCInfo = New FBaseProperty With {.id = t.Item("id"), .FName = t.Item("FName"), .FCName = t.Item("FCName")}
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("取客户信息失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_增加客户"
    Public Function _BILL_Prop_AddNewCustom(ByVal FSession As String, ByRef FCInfo As FBaseProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_AddNewCustom
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FCInfo Is Nothing Then
            ErrString = FControl.GetErrString("增加客户失败!", "参数错误")
            Return False
        End If
        '--检查数据完整性
        If FCInfo.FName.Trim = String.Empty Then
            ErrString = FControl.GetErrString("增加客户失败!", "客户名不能为空!")
            Return False
        End If
        Try
            Dim sqlstr As String = String.Format("select * from Base_Property where FName='{0}' and FType=2", FCInfo.FName.Trim)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("增加客户失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count > 0 Then
                ErrString = FControl.GetErrString("增加客户失败!", "客户名称已经存在!")
                Return False
            End If
            '--增加客户
            sqlstr = String.Format("insert into Base_Property(FName,FCName,FType) values('{0}','{1}',2)", FCInfo.FName, FCInfo.FCName)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = FControl.GetErrString("增加客户失败!", ErrString)
                Return False
            Else
                '--查询客户ID
                sqlstr = String.Format("select id from Base_Property where FName='{0}' and FType=2", FCInfo.FName)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = FControl.GetErrString("取客户ID失败!", ErrString)
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = FControl.GetErrString("取客户ID失败!", "内部服务器错误")
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = FControl.GetErrString("取客户ID失败!", "未知错误")
                    Return False
                End If
                FCInfo.id = ds.Tables(0).Rows(0).Item("id")
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("增加客户失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_删除客户"
    Public Function _BILL_Prop_DeleteCustom(ByVal FSession As String, ByVal FID As String, ByVal FName As String, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_DeleteCustom
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FID.Trim = String.Empty And FName.Trim = String.Empty Then
            ErrString = FControl.GetErrString("删除客户失败!", "参数不正确!")
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If FID.Trim <> String.Empty Then
                '--按ID删除
                '-检查引用
                sqlstr = "select count(*) as a from Bill_Property where FCid=" & FID.Trim
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = FControl.GetErrString("删除客户失败!", "内部服务器错误")
                    Return False
                End If
                If ds.Tables(0).Rows.Count > 0 Then
                    ErrString = FControl.GetErrString("删除客户失败!", "此客户已有业务往来,不能删除!")
                    Return False
                End If
                '--删除
                sqlstr = "delete Base_Property where id=" & FID.Trim
            ElseIf FName.Trim <> String.Empty Then
                '--按客户名
            Else
                ErrString = FControl.GetErrString("删除客户失败!", "参数不正确")
                Return False
            End If
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = FControl.GetErrString("删除客户失败!", ErrString)
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("删除客户失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_修改客户信息"
    Public Function _BILL_Prop_ModifyCustom(ByVal FSession As String, ByVal FCInfo As FBaseProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_ModifyCustom
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FCInfo Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FCInfo.id = 0 Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim sqlstr As String = "select * from Base_Property where id=" & FCInfo.id
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("修改客户失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("修改客户失败!", "未找到指定项目")
                Return False
            End If
            '--修改
            sqlstr = String.Format("Update Base_Property set FName='{0}',FCName={1} where id={2}", FCInfo.FName, FCInfo.FCName, FCInfo.id)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = FControl.GetErrString("修改客户失败!", ErrString)
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("修改客户失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "判断客户信息"
    Private Function _CheckPropCustom(ByVal FName As String, ByVal FCName As String, ByRef FCid As Integer, ByRef ErrString As String) As Boolean
        Dim sqlstr = "select * from Base_Property"
        If FName.Trim = String.Empty Then
            ErrString = "客户信息不正确!"
            FCid = -1
            Return False
        End If
        Try
            sqlstr += String.Format(" where FName='{0}' FType=0", FName.Trim)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = "检查客户信息失败!"
                FCid = -1
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                '--新增
                If FCid = Nothing Then
                    FCid = 0
                End If
                sqlstr = String.Format("insert into Base_Property (FName,FCName,FType) values('{0}','{1}',0)", FName.Trim, FCName.Trim)
            Else
                '--修改
                FCid = ds.Tables(0).Rows(0).Item("id")
                sqlstr = String.Format("update Base_Property set FName='{0}',FCName='{1}' where FName='{0}' and FType=0", FName.Trim, FCName.Trim)
            End If
            Return DControl.ExecSQL(sqlstr, ErrString)
        Catch ex As Exception
            ErrString = String.Format("处理客户信息时出错!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_获取项目列表"
    Public Function _Bill_Prop_GetItemList(ByVal FSession As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._Bill_Prop_GetItemList
        If FStyle.Trim = String.Empty Then
            ErrString = "参数不正确"
            Return False
        End If
        If FSession Is Nothing Then
            ErrString = "验证用户失败!"
            Return False
        End If
        '--验证用户
        If DControl._GetUserBySession(FSession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            Select Case FStyle.Trim
                Case "*"
                    sqlstr = "select * from Base_Property where FType=1"
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = String.Format("获取项目信息失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_获取项目信息"
    Public Function _BILL_Prop_GetItemInfo(ByVal FSession As String, ByVal FID As String, ByVal FName As String, ByRef FIInfo As FBaseProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_GetItemInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FID.Trim = String.Empty And FName.Trim = String.Empty Then
            ErrString = FControl.GetErrString("取项目信息失败!", "参数不正确")
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Base_Property"
            Dim ds As New DataSet
            If FID.Trim <> String.Empty Then
                '--按ID查
                sqlstr += " where id=" & FID.Trim
            ElseIf FName.Trim <> String.Empty Then
                '--按项目名查
                sqlstr += String.Format(" where FType=1 and FName='{0}'", FName.Trim)
            Else
                ErrString = FControl.GetErrString("取项目信息失败!", "参数不正确")
                Return False
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("取项目信息失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("取项目信息失败!", "未找到指定的项目")
                Return False
            End If
            Dim t As DataRow = ds.Tables(0).Rows(0)
            FIInfo = New FBaseProperty With {.id = t.Item("id"), .FName = t.Item("FName"), .FPrice = t.Item("FPrice"), .FQty = t.Item("FQty")}
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("取项目信息失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_增加项目"
    Public Function _BILL_AddNewItem(ByVal FSession As String, ByRef FIInfo As FBaseProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_AddNewItem
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FIInfo Is Nothing Then
            ErrString = FControl.GetErrString("增加项目失败!", "参数错误")
            Return False
        End If
        '--检查必录项
        If FIInfo.FName.Trim = String.Empty Then
            ErrString = FControl.GetErrString("增加项目失败!", "项目名称不能为空")
            Return False
        End If
        Try
            Dim sqlstr As String = String.Format("select * from Base_Property where FName='{0}' and FType=1", FIInfo.FName)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("增加项目失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count > 0 Then
                ErrString = FControl.GetErrString("增加项目失败!", "项目已经存在!")
                Return False
            End If
            '--增加
            sqlstr = String.Format("Insert into Base_Property(FName,FQty,FPrice,FType) values('{0}','{1}','{2}',1)", FIInfo.FName, FIInfo.FQty, FIInfo.FPrice)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = FControl.GetErrString("增加项目失败!", ErrString)
                Return False
            Else
                '--查询ID
                sqlstr = String.Format("select id from Base_Property where FName='{0}' and FType=1", FIInfo.FName)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = FControl.GetErrString("取项目ID失败!", ErrString)
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = FControl.GetErrString("取项目ID失败!", "内部服务器错误")
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = FControl.GetErrString("取项目ID失败!", "未知错误")
                    Return False
                End If
                FIInfo.id = ds.Tables(0).Rows(0).Item("id")
                Return True
            End If
        Catch ex As Exception
            FControl.GetErrString("增加项目失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_修改项目信息"
    Public Function _BILL_Prop_ModifyItem(ByVal FSession As String, ByVal FCInfo As FBaseProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_ModifyItem
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FCInfo Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FCInfo.id = 0 Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim sqlstr As String = "select * from Base_Property where id=" & FCInfo.id
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("修改项目失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("修改项目失败!", "未找到指定项目")
                Return False
            End If
            '--修改
            sqlstr = String.Format("Update Base_Property set FName='{0}',FQty={1},FPrice={2} where id={3}", FCInfo.FName, FCInfo.FQty, FCInfo.FPrice, FCInfo.id)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = FControl.GetErrString("修改项目失败!", ErrString)
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("修改项目失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_删除项目"
    Public Function _BILL_Prop_DeleteItem(ByVal FSession As String, ByVal FID As String, ByVal FName As String, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_DeleteItem
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FID.Trim = String.Empty And FName.Trim = String.Empty Then
            ErrString = FControl.GetErrString("删除项目失败!", "参数不正确!")
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If FID.Trim <> String.Empty Then
                '--按ID删除
                '-检查引用
                sqlstr = String.Format("select count(*) as a from Bill_Property where FItem1={0} or FItem2={0} or FItem3={0} or FItem4={0}", FID.Trim)
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = FControl.GetErrString("删除项目失败!", "内部服务器错误")
                    Return False
                End If
                If ds.Tables(0).Rows.Count > 0 Then
                    ErrString = FControl.GetErrString("删除项目失败!", "此项目已有业务往来,不能删除!")
                    Return False
                End If
                '--删除
                sqlstr = "delete Base_Property where id=" & FID.Trim
            ElseIf FName.Trim <> String.Empty Then
                '--按项目名
            Else
                ErrString = FControl.GetErrString("删除项目失败!", "参数不正确")
                Return False
            End If
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = FControl.GetErrString("删除项目失败!", ErrString)
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("删除项目失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region

#Region "物业单_保存"
    Public Function _Bill_Prop_SaveBill(ByVal FSession As String, ByRef FBinf As FBillProperty, ByRef ErrString As String) As Boolean Implements IService._Bill_Prop_SaveBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FBinf Is Nothing Then
            ErrString = "参数不正确!"
            Return False
        End If
        Try
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            If FBinf.id > 0 Then
                '--检查审核权限
                If FBinf.FUid = FUinf.id Then
                    '--自己的单据
                    If _User_CheckAuth(FSession, "BILL", "FPBILLVERIFYMY", ErrString) = False Then
                        ErrString = "没有权限[审核自己录入的物业单据]"
                        Return False
                    End If
                Else
                    '--其它人的单据
                    If _User_CheckAuth(FSession, "BILL", "FPBILLVERIFY", ErrString) = False Then
                        ErrString = "没有权限[审核自己录入的物业单据]"
                        Return False
                    End If
                End If
            Else
                '--新单据
                If _User_CheckAuth(FSession, "BILL", "FPBILLNEW", ErrString) = False Then
                    ErrString = "没有权限[录入物业单据]"
                    Return False
                End If
            End If
            '--保存单据
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If FBinf.id > 0 Then
                '--检查分组
                Dim FUids As String() = Nothing
                If GetUserPair(FUinf.id, FUids, ErrString) = False Then
                    FBinf.FVid = Nothing
                    FBinf.FVName = Nothing
                    Return False
                End If
                If FUids.Contains(FBinf.FUid) = False Then
                    ErrString = "与制单人不在同一组,无法执行收费操作!"
                    FBinf.FVid = Nothing
                    FBinf.FVName = Nothing
                    Return False
                End If
                '--审核
                sqlstr = String.Format("update Bill_Property set FVid={0},FVName='{1}' where id={2}", FUinf.id, FUinf.FRName, FBinf.id)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    ErrString = String.Format("单据收费失败!{0}错误信息:{1}", vbCrLf, ErrString)
                    Return False
                End If
                Return True
            Else
                '--保存
                '--判断数据
                Dim FCinfo As FBaseProperty = Nothing
                '--判断客户
                If FBinf.FName.Trim = String.Empty Then
                    ErrString = "客户信息不正确"
                    Return False
                End If
                If _CheckPropCustom(FBinf.FName, FBinf.FCName, FBinf.FCid, ErrString) Then
                    Return False
                End If
                'If FBinf.FCid = 0 Then
                '    ErrString = "客户信息不正确"
                '    Return False
                'End If
                'If FBinf.FCid = -1 Then
                '    FCinfo = New FBaseProperty With {.FName = FBinf.FName, .FCName = FBinf.FCName}
                '    If _BILL_Prop_AddNewCustom(FSession, FCinfo, ErrString) = False Then
                '        Return False
                '    End If
                '    If FCinfo.id = Nothing Then
                '        ErrString = "增加新客户失败!"
                '        Return False
                '    End If
                '    FBinf.FCid = FCinfo.id
                'Else
                '    '--更新原有客户
                '    _BILL_Prop_ModifyCustom(FSession, New FBaseProperty With {.id = FBinf.FCid, .FName = FBinf.FName, .FCName = FBinf.FCName}, ErrString)
                'End If
                '--判断分录
                '-第一行
                If Not FBinf.FItemName1 Is Nothing Then
                    '--有数据
                    If FBinf.FItem1 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    If FBinf.FItem1 = -1 Then
                        '--项目需要新增
                        FCinfo = New FBaseProperty With {.FName = FBinf.FItemName1, .FPrice = FBinf.FPrice1, .FQty = FBinf.FQty1}
                        If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                            Return False
                        End If
                        If FCinfo.id = Nothing Then
                            ErrString = "新增项目失败!"
                            Return False
                        End If
                        FBinf.FItem1 = FCinfo.id
                    Else
                        '--更新原有项目
                        _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem1, .FName = FBinf.FItemName1, .FQty = FBinf.FQty1, .FPrice = FBinf.FPrice1}, ErrString)
                    End If
                End If
                '-第二行
                If Not FBinf.FItemName2 Is Nothing Then
                    '--有数据
                    If FBinf.FItem2 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    If FBinf.FItem2 = -1 Then
                        '--项目需要新增
                        FCinfo = New FBaseProperty With {.FName = FBinf.FItemName2, .FPrice = FBinf.FPrice2, .FQty = FBinf.FQty2}
                        If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                            Return False
                        End If
                        If FCinfo.id = Nothing Then
                            ErrString = "新增项目失败!"
                            Return False
                        End If
                        FBinf.FItem2 = FCinfo.id
                    Else
                        '--更新原有项目
                        _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem2, .FName = FBinf.FItemName2, .FQty = FBinf.FQty2, .FPrice = FBinf.FPrice2}, ErrString)
                    End If
                End If
                '-第三行
                If Not FBinf.FItemName3 Is Nothing Then
                    '--有数据
                    If FBinf.FItem3 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    If FBinf.FItem3 = -1 Then
                        '--项目需要新增
                        FCinfo = New FBaseProperty With {.FName = FBinf.FItemName3, .FPrice = FBinf.FPrice3, .FQty = FBinf.FQty3}
                        If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                            Return False
                        End If
                        If FCinfo.id = Nothing Then
                            ErrString = "新增项目失败!"
                            Return False
                        End If
                        FBinf.FItem3 = FCinfo.id
                    Else
                        '--更新原有项目
                        _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem3, .FName = FBinf.FItemName3, .FQty = FBinf.FQty3, .FPrice = FBinf.FPrice3}, ErrString)
                    End If
                End If
                '-第四行
                If Not FBinf.FItemName4 Is Nothing Then
                    '--有数据
                    If FBinf.FItem4 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    If FBinf.FItem4 = -1 Then
                        '--项目需要新增
                        FCinfo = New FBaseProperty With {.FName = FBinf.FItemName4, .FPrice = FBinf.FPrice4, .FQty = FBinf.FQty4}
                        If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                            Return False
                        End If
                        If FCinfo.id = Nothing Then
                            ErrString = "新增项目失败!"
                            Return False
                        End If
                        FBinf.FItem4 = FCinfo.id
                    Else
                        '--更新原有项目
                        _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem4, .FName = FBinf.FItemName4, .FQty = FBinf.FQty4, .FPrice = FBinf.FPrice4}, ErrString)
                    End If
                End If
                '--组合单据号
                Dim FBLSH As Integer = 0
                If FBinf.FBillNo Is Nothing Then
                    If GetBillMaxNumber(1, FUinf.FOrgid, FBLSH, ErrString) = False Then
                        ErrString = String.Format("取单据号失败!{0}错误信息:{1}", vbCrLf, ErrString)
                        Return False
                    End If
                    FBinf.FBillNo = FBinf.FDate.ToString("yyyyMMdd") & FBLSH.ToString("000000")
                    FBinf.FVerifyCode = Int(FBinf.FAmount * CDbl(FBLSH.ToString("0000")) / CDbl(FBinf.FDate.ToString("yyyyMMdd")) * 987654)
                End If
                If FBinf.FBCode Is Nothing Then
                    FBinf.FBCode = Guid.NewGuid.ToString.ToUpper
                End If

                '--单据条目数
                Dim FItem As String = String.Empty
                Dim FItemV As String = String.Empty
                Dim FSDate As String = String.Empty
                Dim FEDate As String = String.Empty
                Dim FQty As String = String.Empty
                Dim FPrice As String = String.Empty
                Dim FDays As String = String.Empty
                Dim FAcc As String = String.Empty
                Dim FItemName As String = String.Empty
                Dim FSDateV As String = String.Empty
                Dim FEDateV As String = String.Empty
                Dim FQtyV As String = String.Empty
                Dim FPriceV As String = String.Empty
                Dim FDaysV As String = String.Empty
                Dim FAccV As String = String.Empty
                Dim FItemNameV As String = String.Empty
                '-第一条
                If FBinf.FItem1 > 0 Then
                    FItem += ",FItem1"
                    FItemV += String.Format(",'{0}'", FBinf.FItem1)
                    FSDate += ",FSDate1"
                    FSDateV += String.Format(",'{0}'", FBinf.FSDate1.Date.ToString("yyyy-MM-dd"))
                    FEDate += ",FEDate1"
                    FEDateV += String.Format(",'{0}'", FBinf.FEDate1.Date.ToString("yyyy-MM-dd"))
                    FQty += ",FQty1"
                    FQtyV += String.Format(",'{0}'", FBinf.FQty1)
                    FPrice += ",FPrice1"
                    FPriceV += String.Format(",'{0}'", FBinf.FPrice1)
                    FDays += ",FDays1"
                    FDaysV += String.Format(",'{0}'", FBinf.FDays1)
                    FAcc += ",FAcc1"
                    FAccV += String.Format(",'{0}'", FBinf.FAcc1)
                    FItemName += ",FItemName1"
                    FItemNameV += String.Format(",'{0}'", FBinf.FItemName1)
                End If
                '-第二条
                If FBinf.FItem2 > 0 Then
                    FItem += ",FItem2"
                    FItemV += String.Format(",'{0}'", FBinf.FItem2)
                    FSDate += ",FSDate2"
                    FSDateV += String.Format(",'{0}'", FBinf.FSDate2.Date.ToString("yyyy-MM-dd"))
                    FEDate += ",FEDate2"
                    FEDateV += String.Format(",'{0}'", FBinf.FEDate2.Date.ToString("yyyy-MM-dd"))
                    FQty += ",FQty2"
                    FQtyV += String.Format(",'{0}'", FBinf.FQty2)
                    FPrice += ",FPrice2"
                    FPriceV += String.Format(",'{0}'", FBinf.FPrice2)
                    FDays += ",FDays2"
                    FDaysV += String.Format(",'{0}'", FBinf.FDays2)
                    FAcc += ",FAcc2"
                    FAccV += String.Format(",'{0}'", FBinf.FAcc2)
                    FItemName += ",FItemName2"
                    FItemNameV += String.Format(",'{0}'", FBinf.FItemName2)
                End If
                '-第三条
                If FBinf.FItem3 > 0 Then
                    FItem += ",FItem3"
                    FItemV += String.Format(",'{0}'", FBinf.FItem3)
                    FSDate += ",FSDate3"
                    FSDateV += String.Format(",'{0}'", FBinf.FSDate3.Date.ToString("yyyy-MM-dd"))
                    FEDate += ",FEDate3"
                    FEDateV += String.Format(",'{0}'", FBinf.FEDate3.Date.ToString("yyyy-MM-dd"))
                    FQty += ",FQty3"
                    FQtyV += String.Format(",'{0}'", FBinf.FQty3)
                    FPrice += ",FPrice3"
                    FPriceV += String.Format(",'{0}'", FBinf.FPrice3)
                    FDays += ",FDays3"
                    FDaysV += String.Format(",'{0}'", FBinf.FDays3)
                    FAcc += ",FAcc3"
                    FAccV += String.Format(",'{0}'", FBinf.FAcc3)
                    FItemName += ",FItemName3"
                    FItemNameV += String.Format(",'{0}'", FBinf.FItemName3)
                End If
                '-第四条
                If FBinf.FItem4 > 0 Then
                    FItem += ",FItem4"
                    FItemV += String.Format(",'{0}'", FBinf.FItem4)
                    FSDate += ",FSDate4"
                    FSDateV += String.Format(",'{0}'", FBinf.FSDate4.Date.ToString("yyyy-MM-dd"))
                    FEDate += ",FEDate4"
                    FEDateV += String.Format(",'{0}'", FBinf.FEDate4.Date.ToString("yyyy-MM-dd"))
                    FQty += ",FQty4"
                    FQtyV += String.Format(",'{0}'", FBinf.FQty4)
                    FPrice += ",FPrice4"
                    FPriceV += String.Format(",'{0}'", FBinf.FPrice4)
                    FDays += ",FDays4"
                    FDaysV += String.Format(",'{0}'", FBinf.FDays4)
                    FAcc += ",FAcc4"
                    FAccV += String.Format(",'{0}'", FBinf.FAcc4)
                    FItemName += ",FItemName4"
                    FItemNameV += String.Format(",'{0}'", FBinf.FItemName4)
                End If
                '--组合语句
                sqlstr = String.Format("insert into Bill_Property (FCid,FName,FCName,FBillNo,FVerifyCode,FDate,FRDate{7}{9}{11}{13}{15}{17}{19},FAmount,FUid,FUName,FBCode{21},FOrgid) values('{0}','{1}','{2}','{3}','{4}','{5}',{6}{8}{10}{12}{14}{16}{18}{20},'{23}','{24}','{25}','{26}'{22},{27})", FBinf.FCid, FBinf.FName, FBinf.FCName, FBinf.FBillNo, FBinf.FVerifyCode, FBinf.FDate, "GETDATE()", FItem, FItemV, FSDate, FSDateV, FEDate, FEDateV, FQty, FQtyV, FPrice, FPriceV, FDays, FDaysV, FAcc, FAccV, FItemName, FItemNameV, FBinf.FAmount, FBinf.FUid, FBinf.FUName, FBinf.FBCode, FUinf.FOrgid)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    ErrString = String.Format("保存单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                    Return False
                End If
                '--取单据ID
                sqlstr = String.Format("select id from Bill_Property where FBCode='{0}'", FBinf.FBCode)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = "保存单据成功但未能取得单据内码"
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = "保存单据成功但未能取得单据内码"
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = "保存单据成功但未能取得单据内码"
                    Return False
                End If
                FBinf.id = ds.Tables(0).Rows(0).Item("id")
                Return True
            End If
        Catch ex As Exception
            ErrString = String.Format("保存单据时发生错误!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_作废"
    Public Function _Bill_Prop_BillCancel(ByVal FSession As String, ByVal FBid As Integer, ByRef ErrString As String) As Boolean Implements IService._Bill_Prop_BillCancel
        If FSession Is Nothing Then
            ErrString = "验证用户失败!"
            Return False
        End If
        '--验证用户
        If DControl._GetUserBySession(FSession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        If FBid = Nothing Then
            ErrString = "参数不正确!"
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Bill_Property where id=" & FBid
            Dim FBillNo As String = String.Empty
            Dim FUInf As New FUserInfo
            Dim ds As New DataSet
            If GetUserInfoBySession(FSession, FUInf, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:取单据信息时出错 {1}", vbCrLf, ErrString)
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = String.Format("作废单据失败!{0}错误信息:内部服务器错误", vbCrLf)
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = String.Format("作废单据失败!{0}错误信息:无法找到指定的记录", vbCrLf)
                Return False
            End If
            FBillNo = ds.Tables(0).Rows(0).Item("FBillNo")
            If ds.Tables(0).Rows(0).Item("FVid") <> Nothing And FUInf.id <> 1 Then
                ErrString = "只有管理员可以作废已收费的单据!"
                Return False
            End If
            '--检查权限
            If ds.Tables(0).Rows(0).Item("FUid") = FUInf.id Then
                '--自己的单据
                If _User_CheckAuth(FSession, "BILL", "FPBILLCANCEL", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            Else
                '--其它人的单据
                If _User_CheckAuth(FSession, "BILL", "FPBILLCANCELA", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            End If
            '--开始作废
            sqlstr = "update Bill_Property set FCancel=-1 where id=" & FBid
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            '--写日志
            DControl.WriteSysLog("BILL", FUInf.id, FUInf.FRName, "作废单据", "作废物业收据 单号:" & FBillNo, 1)
            Return True
        Catch ex As Exception
            ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_获取单据信息"
    Public Function _BILL_Prop_GetBillInfo(ByVal FSession As String, ByVal FBid As String, ByVal FBillNo As String, ByRef FBInfo As FBillProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_GetBillInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FBid.Trim = String.Empty And FBillNo.Trim = String.Empty Then
            ErrString = FControl.GetErrString("取单据信息失败!", "参数不正确")
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Bill_Property where"
            Dim ds As New DataSet
            If FBid.Trim <> String.Empty Then
                '--按ID查询
                sqlstr += " id=" & FBid.Trim
            ElseIf FBillNo.Trim <> String.Empty Then
                '--按单据号查询
            Else
                FControl.GetErrString("取单据信息失败!", "参数不正确!")
                Return False
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("取单据信息失败!", "内部服务器错误!")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("取单据信息失败!", "未找到指定项目")
                Return False
            End If
            '--用户信息
            Dim FUInf As New FUserInfo
            If GetUserInfoBySession(FSession, FUInf, ErrString) = False Then
                ErrString = FControl.GetErrString("取单据信息失败!", ErrString)
                Return False
            End If
            '--检查权限
            If ds.Tables(0).Rows(0).Item("FUid") <> FUInf.id Then
                If _User_CheckAuth(FSession, "BILL", "FPBILLVIEW", "") = False Then
                    ErrString = "没有权限!"
                    Return False
                End If
            End If
            '--写数据项
            Dim r As DataRow = ds.Tables(0).Rows(0)
            FBInfo.id = FControl.cvN(r.Item("id"))
            FBInfo.FCid = FControl.cvN(r.Item("FCid"))
            FBInfo.FName = FControl.cvN(r.Item("FName"))
            FBInfo.FCName = FControl.cvN(r.Item("FCName"))
            FBInfo.FBillNo = FControl.cvN(r.Item("FBillNo"))
            FBInfo.FVerifyCode = FControl.cvN(r.Item("FVerifyCode"))
            FBInfo.FDate = FControl.cvN(r.Item("FDate"))
            FBInfo.FRDate = FControl.cvN(r.Item("FRDate"))
            FBInfo.FItem1 = FControl.cvN(r.Item("FItem1"))
            FBInfo.FItemName1 = FControl.cvN(r.Item("FItemName1"))
            FBInfo.FItem2 = FControl.cvN(r.Item("FItem2"))
            FBInfo.FItemName2 = FControl.cvN(r.Item("FItemName2"))
            FBInfo.FItem3 = FControl.cvN(r.Item("FItem3"))
            FBInfo.FItem4 = FControl.cvN(r.Item("FItem4"))
            FBInfo.FItemName3 = FControl.cvN(r.Item("FItemName3"))
            FBInfo.FItemName4 = FControl.cvN(r.Item("FItemName4"))
            FBInfo.FSDate1 = FControl.cvN(r.Item("FSDate1"))
            FBInfo.FSDate2 = FControl.cvN(r.Item("FSDate2"))
            FBInfo.FSDate3 = FControl.cvN(r.Item("FSDate3"))
            FBInfo.FSDate4 = FControl.cvN(r.Item("FSDate4"))
            FBInfo.FEDate1 = FControl.cvN(r.Item("FEDate1"))
            FBInfo.FEDate2 = FControl.cvN(r.Item("FEDate2"))
            FBInfo.FEDate3 = FControl.cvN(r.Item("FEDate3"))
            FBInfo.FEDate4 = FControl.cvN(r.Item("FEDate4"))
            FBInfo.FQty1 = FControl.cvN(r.Item("FQty1"))
            FBInfo.FQty2 = FControl.cvN(r.Item("FQty2"))
            FBInfo.FQty3 = FControl.cvN(r.Item("FQty3"))
            FBInfo.FQty4 = FControl.cvN(r.Item("FQty4"))
            FBInfo.FPrice1 = FControl.cvN(r.Item("FPrice1"))
            FBInfo.FPrice2 = FControl.cvN(r.Item("FPrice2"))
            FBInfo.FPrice3 = FControl.cvN(r.Item("FPrice3"))
            FBInfo.FPrice4 = FControl.cvN(r.Item("FPrice4"))
            FBInfo.FDays1 = FControl.cvN(r.Item("FDays1"))
            FBInfo.FDays2 = FControl.cvN(r.Item("FDays2"))
            FBInfo.FDays3 = FControl.cvN(r.Item("FDays3"))
            FBInfo.FDays4 = FControl.cvN(r.Item("FDays4"))
            FBInfo.FAcc1 = FControl.cvN(r.Item("FAcc1"))
            FBInfo.FAcc2 = FControl.cvN(r.Item("FAcc2"))
            FBInfo.FAcc3 = FControl.cvN(r.Item("FAcc3"))
            FBInfo.FAcc4 = FControl.cvN(r.Item("FAcc4"))
            FBInfo.FAmount = FControl.cvN(r.Item("FAmount"))
            FBInfo.FUid = FControl.cvN(r.Item("FUid"))
            FBInfo.FUName = FControl.cvN(r.Item("FUName"))
            FBInfo.FVid = FControl.cvN(r.Item("FVid"))
            FBInfo.FVName = FControl.cvN(r.Item("FVName"))
            FBInfo.FCancel = FControl.cvN(r.Item("FCancel"))
            FBInfo.FPCount = FControl.cvN(r.Item("FPCount"))
            FBInfo.FBCode = FControl.cvN(r.Item("FBCode"))
            FBInfo.FOrgid = FControl.cvN(r.Item("FOrgid"))
            Return True
        Catch ex As Exception
            FControl.GetErrString("取单据信息失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_增加打印次数"
    Public Function _Bill_Prop_AddPrintCount(ByVal FSession As String, ByRef FBInf As FBillProperty, ByRef ErrString As String) As Boolean Implements IService._Bill_Prop_AddPrintCount
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBInf Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FBInf.id = 0 Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = "update Bill_Property set FPCount=FPCount+1 output inserted.FPCount where FCancel=0 and id=" & FBInf.id
            Dim ds As New DataSet
            If FBInf.FVid <> FUinf.id Then
                '--打印其他人审核单据,检查权限
                ErrString = "只能打印自己收费的单据"
                Return False
            Else
                '--打印自己审核的单据
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败", "无法找到相应单据")
                Return False
            End If
            FBInf.FPCount = ds.Tables(0).Rows(0).Item("FPCount")
            Return True
        Catch ex As Exception
            FControl.GetErrString("打印失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_查询"
    Public Function _BILL_Prop_GetBillList(ByVal FSession As String, ByVal FSDate As Date, ByVal FEDate As Date, ByVal FName As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_GetBillList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = String.Empty
            Dim ConFUid As String = " and FUid="
            Dim ConFName As String = " and FName like"
            '--检查权限
            Dim FIsAuth As Boolean = _User_CheckAuth(FSession, "BILL", "FPBILLVIEW", "")
            If FIsAuth Then
                '--所有
                ConFUid = Nothing
            Else
                '--自己
                ConFUid += FUinf.id.ToString
            End If
            '--检查单位名
            If Not FName Is Nothing Then
                If FName.Trim <> String.Empty Then
                    ConFName = String.Format(" and FName like '%{0}'", FName.Trim)
                Else
                    ConFName = Nothing
                End If
            Else
                ConFName = Nothing
            End If
            '--制单单位
            Dim FOrg As String = Nothing
            If FUinf.id = 1 Then
                FOrg = ""
            Else
                FOrg = " and FOrgid=" & FUinf.FOrgid
            End If
            DControl.ExecSQL("update t set t.FOrgName=t1.FName from Bill_Property t,Base_Org t1 where t.FOrgid=t1.id", "")
            Select Case FStyle
                Case "*"
                    sqlstr = String.Format("select id '内码',FName '交款单位',FCName '交款人',FBillNo '单号',FVerifyCode '验证码',FDate '日期',FItemName1 '项目1名称',convert(varchar(120),FSDate1,23) + ' ~ ' + convert(varchar(120),FEDate1,23) '收费区间1',FQty1 '平米数1',FPrice1 '单价1',FDays1 '天数1',FAcc1 '金额1',FItemName2 '项目2名称',convert(varchar(120),FSDate2,23) + ' ~ ' + convert(varchar(120),FEDate2,23) '收费区间2',FQty2 '平米数2',FPrice2 '单价2',FDays2 '天数2',FAcc2 '金额2',FItemName3 '项目3名称',convert(varchar(120),FSDate3,23) + ' ~ ' + convert(varchar(120),FEDate3,23) '收费区间3',FQty3 '平米数3',FPrice3 '单价3',FDays3 '天数3',FAcc3 '金额3',FItemName4 '项目4名称',convert(varchar(120),FSDate4,23) + ' ~ ' + convert(varchar(120),FEDate4,23) '收费区间4',FQty4 '平米数4',FPrice4 '单价4',FDays4 '天数4',FAcc4 '金额4',FAmount '总金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Property where FDate BETWEEN '{0}' and '{1}'{2}{3}{4}", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "*ZF"
                    sqlstr = String.Format("select id '内码',FName '交款单位',FCName '交款人',FBillNo '单号',FVerifyCode '验证码',FDate '日期',FItemName1 '项目1名称',convert(varchar(120),FSDate1,23) + ' ~ ' + convert(varchar(120),FEDate1,23) '收费区间1',FQty1 '平米数1',FPrice1 '单价1',FDays1 '天数1',FAcc1 '金额1',FItemName2 '项目2名称',convert(varchar(120),FSDate2,23) + ' ~ ' + convert(varchar(120),FEDate2,23) '收费区间2',FQty2 '平米数2',FPrice2 '单价2',FDays2 '天数2',FAcc2 '金额2',FItemName3 '项目3名称',convert(varchar(120),FSDate3,23) + ' ~ ' + convert(varchar(120),FEDate3,23) '收费区间3',FQty3 '平米数3',FPrice3 '单价3',FDays3 '天数3',FAcc3 '金额3',FItemName4 '项目4名称',convert(varchar(120),FSDate4,23) + ' ~ ' + convert(varchar(120),FEDate4,23) '收费区间4',FQty4 '平米数4',FPrice4 '单价4',FDays4 '天数4',FAcc4 '金额4',FAmount '总金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Property  where FDate BETWEEN '{0}' and '{1}'{2}{3}{4} and FCancel=0", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "CN"
                    sqlstr = String.Format("select id,FBillNo '单号',FVerifyCode '验证码',FName '交款单位',FCName '交款人',FDate '日期',FAmount '金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Property where FDate BETWEEN '{0}' and '{1}'{2}{3}{4}", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "CNZF"
                    sqlstr = String.Format("select id,FBillNo '单号',FVerifyCode '验证码',FName '交款单位',FCName '交款人',FDate '日期',FAmount '金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Property  where FDate BETWEEN '{0}' and '{1}'{2}{3}{4} and FCancel=0", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            '--查询 
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("查询单据失败!", ErrString)
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("查询时出现错误!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "物业单_修改"
    Public Function _BILL_Prop_ModifyBill(ByVal FSession As String, ByRef FBinf As FBillProperty, ByRef ErrString As String) As Boolean Implements IService._BILL_Prop_ModifyBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Dim FUinf As New FUserInfo
        If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
            Return False
        End If
        Try
            '--检查数据
            Dim FCinfo As New FBaseProperty
            '-第一行
            If Not FBinf.FItemName1 Is Nothing Then
                '--有数据
                If FBinf.FItem1 = 0 Then
                    '--无数据
                    ErrString = "参数错误"
                    Return False
                End If
                If FBinf.FItem1 = -1 Then
                    '--项目需要新增
                    FCinfo = New FBaseProperty With {.FName = FBinf.FItemName1, .FPrice = FBinf.FPrice1, .FQty = FBinf.FQty1}
                    If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBinf.FItem1 = FCinfo.id
                Else
                    '--更新原有项目
                    _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem1, .FName = FBinf.FItemName1, .FQty = FBinf.FQty1, .FPrice = FBinf.FPrice1}, ErrString)
                End If
            End If
            '-第二行
            If Not FBinf.FItemName2 Is Nothing Then
                '--有数据
                If FBinf.FItem2 = 0 Then
                    '--无数据
                    ErrString = "参数错误"
                    Return False
                End If
                If FBinf.FItem2 = -1 Then
                    '--项目需要新增
                    FCinfo = New FBaseProperty With {.FName = FBinf.FItemName2, .FPrice = FBinf.FPrice2, .FQty = FBinf.FQty2}
                    If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBinf.FItem2 = FCinfo.id
                Else
                    '--更新原有项目
                    _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem2, .FName = FBinf.FItemName2, .FQty = FBinf.FQty2, .FPrice = FBinf.FPrice2}, ErrString)
                End If
            End If
            '-第三行
            If Not FBinf.FItemName3 Is Nothing Then
                '--有数据
                If FBinf.FItem3 = 0 Then
                    '--无数据
                    ErrString = "参数错误"
                    Return False
                End If
                If FBinf.FItem3 = -1 Then
                    '--项目需要新增
                    FCinfo = New FBaseProperty With {.FName = FBinf.FItemName3, .FPrice = FBinf.FPrice3, .FQty = FBinf.FQty3}
                    If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBinf.FItem3 = FCinfo.id
                Else
                    '--更新原有项目
                    _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem3, .FName = FBinf.FItemName3, .FQty = FBinf.FQty3, .FPrice = FBinf.FPrice3}, ErrString)
                End If
            End If
            '-第四行
            If Not FBinf.FItemName4 Is Nothing Then
                '--有数据
                If FBinf.FItem4 = 0 Then
                    '--无数据
                    ErrString = "参数错误"
                    Return False
                End If
                If FBinf.FItem4 = -1 Then
                    '--项目需要新增
                    FCinfo = New FBaseProperty With {.FName = FBinf.FItemName4, .FPrice = FBinf.FPrice4, .FQty = FBinf.FQty4}
                    If _BILL_AddNewItem(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBinf.FItem4 = FCinfo.id
                Else
                    '--更新原有项目
                    _BILL_Prop_ModifyItem(FSession, New FBaseProperty With {.id = FBinf.FItem4, .FName = FBinf.FItemName4, .FQty = FBinf.FQty4, .FPrice = FBinf.FPrice4}, ErrString)
                End If
            End If

            '--组合单据号
            Dim FBLSH As Integer = Right(FBinf.FBillNo, 4)
            FBinf.FVerifyCode = Int(FBinf.FAmount * CDbl(FBLSH.ToString("0000")) / CDbl(FBinf.FDate.ToString("yyyyMMdd")) * 987654)
            '--单据条目数
            Dim FItem As String = String.Empty
            Dim FSDate As String = String.Empty
            Dim FEDate As String = String.Empty
            Dim FQty As String = String.Empty
            Dim FPrice As String = String.Empty
            Dim FDays As String = String.Empty
            Dim FAcc As String = String.Empty
            Dim FItemName As String = String.Empty
            '-第一条
            If FBinf.FItem1 > 0 Then
                FItem += ",FItem1=" & FBinf.FItem1
                FSDate += String.Format(",FSDate1='{0:yyyy-MM-dd}'", FBinf.FSDate1.Date)
                FEDate += String.Format(",FEDate1='{0:yyyy-MM-dd}'", FBinf.FEDate1.Date)
                FQty += ",FQty1=" & FBinf.FQty1
                FPrice += ",FPrice1=" & FBinf.FPrice1
                FDays += ",FDays1=" & FBinf.FDays1
                FAcc += ",FAcc1=" & FBinf.FAcc1
                FItemName += String.Format(",FItemName1='{0}'", FBinf.FItemName1)
            End If
            '-第二条
            If FBinf.FItem2 > 0 Then
                FItem += ",FItem2=" & FBinf.FItem2
                FSDate += String.Format(",FSDate2='{0:yyyy-MM-dd}'", FBinf.FSDate2.Date)
                FEDate += String.Format(",FEDate2='{0:yyyy-MM-dd}'", FBinf.FEDate2.Date)
                FQty += ",FQty2=" & FBinf.FQty2
                FPrice += ",FPrice2=" & FBinf.FPrice2
                FDays += ",FDays2=" & FBinf.FDays2
                FAcc += ",FAcc2=" & FBinf.FAcc2
                FItemName += String.Format(",FItemName2='{0}'", FBinf.FItemName2)
            End If
            '-第三条
            If FBinf.FItem3 > 0 Then
                FItem += ",FItem3=" & FBinf.FItem3
                FSDate += String.Format(",FSDate3='{0:yyyy-MM-dd}'", FBinf.FSDate3.Date)
                FEDate += String.Format(",FEDate3='{0:yyyy-MM-dd}'", FBinf.FEDate3.Date)
                FQty += ",FQty3=" & FBinf.FQty3
                FPrice += ",FPrice3=" & FBinf.FPrice3
                FDays += ",FDays3=" & FBinf.FDays3
                FAcc += ",FAcc3=" & FBinf.FAcc3
                FItemName += String.Format(",FItemName3='{0}'", FBinf.FItemName3)
            End If
            '-第四条
            If FBinf.FItem4 > 0 Then
                FItem += ",FItem4=" & FBinf.FItem4
                FSDate += String.Format(",FSDate4='{0:yyyy-MM-dd}'", FBinf.FSDate4.Date)
                FEDate += String.Format(",FEDate4='{0:yyyy-MM-dd}'", FBinf.FEDate4.Date)
                FQty += ",FQty4=" & FBinf.FQty4
                FPrice += ",FPrice4=" & FBinf.FPrice4
                FDays += ",FDays4=" & FBinf.FDays4
                FAcc += ",FAcc4=" & FBinf.FAcc4
                FItemName += String.Format(",FItemName4='{0}'", FBinf.FItemName4)
            End If
            Dim sqlstr As String = String.Format("update Bill_Property set FDate='{0}',FVerifyCode='{1}',FAmount='{2}'{3}{4}{5}{6}{7}{8}{9}{10} where id={11}", FBinf.FDate.ToString("yyyy-MM-dd"), FBinf.FVerifyCode, FBinf.FAmount, FItem, FItemName, FSDate, FEDate, FQty, FPrice, FDays, FAcc, FBinf.id)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                Return False
            End If
            DControl.WriteSysLog("BILL", FUinf.id, FUinf.FName, "修改单据", "单据类型:物业收据 单据号:" & FBinf.FBillNo, 0)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region





















End Class
